package com.atguigu.yygh.user.service.impl;

import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.mapper.PatientMapper;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 就诊人表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2022-12-27
 */
@Service
@Slf4j
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Override
    public Patient getPatientById(Long id) {
        Patient patient = baseMapper.selectById(id);
        log.info("getPatientById()" + patient);
        return this.packPatient(patient);
    }

    /**
     * 获取就诊人列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<Patient> findAllByUserId(Long userId) {

        //根据userid查询所有就诊人信息列表
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<Patient> patientList = baseMapper.selectList(wrapper);
        //封装数据
        patientList.stream().forEach(this::packPatient);
        return patientList;
    }

    @Autowired
    private DictFeignClient dictFeignClient;

    /**
     * Patient对象里面其他参数封装
     *
     * @param patient
     * @return
     */
    private Patient packPatient(Patient patient) {

        //根据证件类型编码，获取证件类型名称
        String certificatesTypeString = dictFeignClient.getName(
                DictEnum.CERTIFICATES_TYPE.getDictCode(),
                patient.getCertificatesType());
        patient.getParam().put("certificatesTypeString", certificatesTypeString);

        //区域
        //省
        String provinceString = dictFeignClient.getName(null, patient.getProvinceCode());
        patient.getParam().put("provinceString", provinceString);

        //市
        if (!StringUtils.isEmpty(patient.getCityCode())) {
            String cityString = dictFeignClient.getName(null, patient.getCityCode());
            patient.getParam().put("cityString", cityString);
        }

        //区
        if (!StringUtils.isEmpty(patient.getDistrictCode())) {
            String districtString = dictFeignClient.getName(null, patient.getDistrictCode());
            patient.getParam().put("districtString", districtString);
        }

        //patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());

        return patient;
    }
}
